//
//  ChannelSelectController.m
//  History
//
//  Created by 七 on 16/9/6.
//  Copyright © 2016年 kane. All rights reserved.
//

#import "ChannelSelectController.h"

@interface ChannelSelectController ()
{
    NSInteger currentIndex;
    __weak IBOutlet UITableView *channelTable;
    IBOutlet UIView *BGView;
    __weak IBOutlet UIView *navView;
    __weak IBOutlet UILabel *lineLbl;
    
    
    
    
}
@end

@implementation ChannelSelectController

- (void)viewDidLoad {
    [super viewDidLoad];
    currentIndex = 1;
    channelTable.backgroundColor = KKColor_DarkWhite_Black;
    BGView.backgroundColor = KKColor_BGColor_GrayToDarkgray;
    navView.backgroundColor = KKColor_DarkGray_White;
    lineLbl.backgroundColor = KKColor_NavLineColor;
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"ChannelSelected"] == nil) {
        currentIndex = 0;
    }else{
        currentIndex = [[[NSUserDefaults standardUserDefaults] objectForKey:@"ChannelSelected"] integerValue];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - User Action
- (IBAction)backPress:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark UITableViewDelegate & datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identify = [NSString stringWithFormat:@"%ld%ld",(long)indexPath.section,(long)indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
    }
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.backgroundColor = KKColor_DarkGray_White;
    cell.textLabel.textColor = KKColor_Black_LightGary;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == [[[NSUserDefaults standardUserDefaults] objectForKey:@"ChannelSelected"] integerValue]) {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }else{
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = @"全部频道";
            break;
        case 1:
            cell.textLabel.text = @"手动选择";
            break;
        default:
            break;
    }
    return cell;
}

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row==currentIndex){
        return UITableViewCellAccessoryCheckmark;
    }
    else{
        return UITableViewCellAccessoryNone;
    }
}


-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    currentIndex = indexPath.row;
    [channelTable reloadData];
    switch (currentIndex) {
        case 0:
        case 1:
            [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",currentIndex] forKey:@"ChannelSelected"];
            break;
        default:
            break;
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeChannel" object:nil];
}

@end
